//
//  CommutationControlViewController.swift
//  AlterationSwitch
//
//  Created by solarke_apple_mini_1 on 2018/10/25.
//  Copyright © 2018年 石家庄科林电气股份有限公司. All rights reserved.
//

import UIKit

class CommutationControlViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource{
    var num = 0
    let string = ["[1]A","[2]B","[3]C"]
    var original = 1
    var goal = 1
    var fitst = 0
    // MARK: - IBOutlet
    @IBOutlet weak var measureDotTextField:UITextField!
    @IBOutlet weak var originalPhaseButton:UIButton!
    @IBOutlet weak var goalPhaseButton:UIButton!
    @IBOutlet weak var table:UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        originalPhaseButton.setTitle(string[0], for: .normal)
        goalPhaseButton.setTitle(string[0], for: .normal)
        table.isHidden = true
        table.tableFooterView = UIView(frame: .zero)
        measureDotTextField.text = "1"
        notificationCenter.addObserver(self, selector: #selector(dealWithData(sender:)), name: huanXiangControlNotification, object: nil)
    }
    
    @objc func dealWithData(sender: NSNotification) {
        let info = sender.userInfo as! [String: AnyObject]
        let data = info["data"] as AnyObject
        let suredeny = data.object(forKey: "suredeny") as! String
        if suredeny == "ok" {
            showToast("成功")
        } else {
            showToast("失败")
        }
    }
    
    // MARK: - IBAction
    @IBAction func originalButtonTap() {
        table.isHidden = false
        num = 0
    }
    
    @IBAction func goalButtonTap() {
        table.isHidden = false
        num = 1
    }
    
    @IBAction func settingButtonTap() {
        view.endEditing(true)
        if viery() {
            let measureDot = measureDotTextField.text
            let mpNo:Int32 = Int32(measureDot!)!
            let fromphase:UInt8 = UInt8(original)
            let tophase:UInt8 = UInt8(goal)
            let termaddr:UnsafeMutablePointer<UInt8> = getTermaddr()
            let ctrl_fn003_send = ocFile().ctrl_fn003_send(termaddr, a3: a3, mpNo: mpNo, fromphase: fromphase, tophase: tophase)
            var arr = [UInt8](repeating: 0, count: 37)
            //将dst中的所有元素拷贝到arr中
            for i in 0..<37 {
                arr[i] = UInt8(ctrl_fn003_send[i])
            }
            sendMessageBytes(msg: arr)
        }
    }
    
    func viery() -> Bool {
        let measureDot = measureDotTextField.text
        if (measureDot?.isEmpty)! {
            showToast("请输入测量点号!")
            return false
        }
        
        if original == goal {
            showToast("原相别不能和目标相别相同!")
            return false
        }
        
        return true
    }
    
    @IBAction func backViewTap() {
        table.isHidden = true
        view.endEditing(true)
    }
    
    // MARK: - Table view data source
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = string[indexPath.row]
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(indexPath.row)
        print("didSelectRowAtstring[indexPath.row]===>\(string[indexPath.row])")
        if num == 0 {
            originalPhaseButton.setTitle(string[indexPath.row], for: .normal)
            original = indexPath.row + 1
        } else {
            goalPhaseButton.setTitle(string[indexPath.row], for: .normal)
            goal = indexPath.row + 1
        }
        tableView.isHidden = true
    }
}
